package com.aizuda.boot.modules.business.contract.controller;


import com.aizuda.boot.modules.business.contract.entity.dto.ContractFieldAddDTO;
import com.aizuda.boot.modules.business.contract.entity.dto.ContractFieldDepartmentDTO;
import com.aizuda.boot.modules.business.contract.entity.dto.ContractFieldSortDTO;
import com.aizuda.boot.modules.business.contract.entity.dto.ContractFieldUpdateDTO;
import com.aizuda.boot.modules.business.contract.dto.SingleResponse;
import com.aizuda.boot.modules.business.contract.entity.vo.*;
import com.aizuda.boot.modules.common.constant.enums.ResponseCodeEnum;
import com.aizuda.boot.modules.common.constant.enums.CustomFieldTypeEnum;
import com.aizuda.boot.modules.business.contract.service.ContractFieldService;
import com.aizuda.boot.modules.business.contract.service.impl.ContractExe;
import com.aizuda.boot.modules.system.entity.vo.department.DepartmentTreeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Author sangyirong
 * @ClassName ContractFieldController
 * @Version 1.0
 * @Date 2025/5/29 18:12
 */
@RestController
@RequestMapping("/contract/fields")
@Tag(name = "合同字段管理")
public class ContractFieldController {

    @Resource
    private ContractExe contractExe;

    @Resource
    private ContractFieldService contractFieldService;

    /**
     * 获取合同字段列表
     *
     * @param moduleId
     * @return
     */
    @Operation(summary = "获取合同字段列表")
    @GetMapping("/{moduleId}")
    public SingleResponse<List<ContractFieldShowDTO>> listContractFields(@PathVariable Integer moduleId) {
        return contractExe.listContractFields(moduleId);
    }

    @Operation(summary = "新增合同字段")
    @PostMapping("/{moduleId}")
    public SingleResponse createContractField(@PathVariable Integer moduleId, @RequestBody ContractFieldAddDTO contractFieldAddDTO) {
        return contractExe.createContractField(moduleId, contractFieldAddDTO);
    }

    @Operation(summary = "获取合同字段详情")
    @GetMapping("/{moduleId}/{id}")
    public SingleResponse<ContractFieldDetailShowDTO> getContractFieldById(@PathVariable("moduleId") Integer moduleId, @PathVariable("id") Long id) {
        return contractExe.getContractFieldById(moduleId, id);
    }

    @Operation(summary = "编辑合同字段")
    @PutMapping("/{moduleId}/{id}")
    public SingleResponse updateContractField(@PathVariable("moduleId") Integer moduleId, @PathVariable("id") Long id,
                                              @RequestBody ContractFieldUpdateDTO contractFieldUpdateDTO) {
        return contractExe.updateContractField(moduleId, id, contractFieldUpdateDTO);
    }

    @Operation(summary = "删除合同字段")
    @DeleteMapping("/{moduleId}/{id}")
    public SingleResponse deleteContractField(@PathVariable("moduleId") Integer moduleId, @PathVariable("id") Long id) {
        return contractExe.deleteContractField(moduleId, id);
    }

    @Operation(summary = "合同字段排序")
    @PutMapping("/{moduleId}/sort")
    public SingleResponse sortContractFields(@PathVariable Integer moduleId, @RequestBody ContractFieldSortDTO contractFieldSortDTO) {
        return contractExe.sortContractFields(moduleId, contractFieldSortDTO);
    }

    @Operation(summary = "获取字段类型")
    @GetMapping("/type")
    public SingleResponse getFieldType() {
        return contractExe.getFieldType();
    }

    @Operation(summary = "获取币种")
    @GetMapping("/currency")
    public SingleResponse getCurrency() {
        return contractExe.getCurrency();
    }

    @Operation(summary = "获取字段配置信息")
    @GetMapping("/config")
    public SingleResponse<FieldConfigRespDTO> getFieldConfig() {
        try {
            FieldConfigRespDTO fieldConfig = contractFieldService.getFieldConfig();
            return SingleResponse.of(fieldConfig);
        } catch (Exception e) {
            return SingleResponse.failure(ResponseCodeEnum.SHOW_MSG.getCode(), "获取字段配置失败: " + e.getMessage());
        }
    }

    @Operation(summary = "获取所有模块的字段树")
    @GetMapping("/tree")
    public SingleResponse<List<ModuleFieldTreeDTO>> getAllModuleFieldTree() {
        return contractExe.getAllModuleFieldTree();
    }

    @Operation(summary = "获取自定义字段类型枚举")
    @GetMapping("/custom-field-types")
    public SingleResponse<List<Map<String, String>>> getCustomFieldTypeEnums() {
        List<Map<String, String>> result = new ArrayList<>();
        for (CustomFieldTypeEnum type : CustomFieldTypeEnum.values()) {
            Map<String, String> map = new HashMap<>();
            map.put("key", type.getKey());
            map.put("value", type.getValue());
            result.add(map);
        }
        return SingleResponse.of(result);
    }

    @Operation(summary = "获取全部字段")
    @GetMapping("/all-fields")
    public SingleResponse<List<FieldsVO>> allFields() {
        return contractFieldService.allFields();
    }

    @Operation(summary = "根据父合同类型获取部门树")
    @PostMapping("/department-tree")
    public SingleResponse<List<DepartmentTreeVO>> getDepartmentTreeByParent(@RequestBody ContractFieldDepartmentDTO dto) {
        try {
            return contractExe.getDepartmentTreeByParent(dto);
        } catch (Exception e) {
            return SingleResponse.failure(ResponseCodeEnum.SYS_ERROR.getCode(), ResponseCodeEnum.SYS_ERROR.getMsg());
        }
    }
}
